<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 易付通
 */
class YifutongController extends PayController{
    protected $config;
	protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $ismobile = isMobile();

        $data = [];

        $data['tenantOrderNo'] = $return['orderid'];
        $data['pageUrl'] = $return['callbackurl'];
        $data['notifyUrl'] = $return['notifyurl'];
        $data['amount'] = $return['amount'];

        switch($this->config['paytype']) {
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:
                $data['payType'] = 'alipay';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        $data['remark'] = 'yft';
        $data['userId'] = 'yft123';

        $data['sign'] = $this->getSign($data);

        $res = curl($this->config['gateway'], httpstr($data));

        $res = json_decode($res, true);

        $code = $res['url'];

        if(!$code){
            if($res['msg']){
                $this->showmessage($res['msg'],['code'=>C("THIRDCODES.".$this->class)]);
            }
            $this->showmessage('获取支付链接失败!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("deny_layer",1);
        //$this->assign("wap_url", $code);
        //$this->assign("code_url", $code);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=" . base64_encode($code));
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);

        switch($this->config['paytype']){
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:
                if($ismobile){
                    header("Location:{$code}"); exit;
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    private function getSign($data){
        ksort($data);

        $str = '';
        foreach($data as $k=>$v){
            if($v === '' || $v === null || in_array($k, ['sign'])){
                continue;
            }

            $str .= "{$k}={$v}&";
        }

        $str .= "key=". $this->config['signkey'];

        return strtoupper(md5($str));
    }

    //通知
    public function notifyurl(){
        //$this->debug_callback();
        if(!$this->checkip(getIP(),['47.107.248.129'])){
            exit("wrong");
        }

        $p = I("post.");

        //$p = array ( 'tenantOrderNo' => '2019053122221354845', 'amount' => '500.00', 'status' => '200', 'remark' => 'yft', 'msg' => '交易成功', 'sign' => '882F2DCBA5E363894E1031B89EB0BF94', );

        //获取订单信息
        $orderid = $p['tenantOrderNo'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        /*M("Order")->where(array("pay_orderid"=>$p['busi_order_no']))->save(array(

            //"pay_orderid_rd"=> $p['order_no']
        ));*/

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];
        $sign2 = $this->getSign($p);

        if($sign !== $sign2){
            exit("sign_wrong");
        }

        if($p['status'] !== '200'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],'success');
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}